package com.gla.giliair.interceptor;

import com.gla.giliair.entity.Admin;
import com.gla.giliair.web.vo.AdminVO;
import com.gla.giliair.web.vo.CustomerVO;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/9/1 10:33
 */
public class LoginInterceptor implements HandlerInterceptor {
    private static final String loginPage = "/vueTest/pages/login.html";
    private static final String adminLoginPage = "/gilipanel/login.html";
    private static final String failMsg1 = "{\n" +
            "  \"msg\": \"用户未登录！\",\n" +
            "  \"status\": 551\n" +
            "}";
    private static final String failMsg2 = "{\n" +
            "  \"msg\": \"管理员未登录！\",\n" +
            "  \"status\": 552\n" +
            "}";
    private static final String failMsg3 = "{\n" +
            "  \"msg\": \"管理员权限不足！\",\n" +
            "  \"status\": 553\n" +
            "}";

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        HttpSession httpSession = request.getSession();
        System.out.println("拦截器拦截的session：" + httpSession);
        String uri = request.getRequestURI();
        System.out.println("拦截器拦截的uri：" + uri);
        //只拦截/api/user/开头的接口
        if (uri.startsWith("/api/user/")) {
            CustomerVO user = (CustomerVO) httpSession.getAttribute("user");
            if (user == null) {
                response.setContentType("application/json; charset=utf-8");
                response.getWriter().println(failMsg1);
                //response.sendRedirect(loginPage);
                return false;
            }
            System.out.println("拦截器拦截的用户：" + user + "  " + user.getUsername());
        }
        //只拦截/api/admin/开头的接口
        if (uri.startsWith("/api/admin/")) {
            AdminVO user = (AdminVO) httpSession.getAttribute("admin");
            if (user == null) {
                response.setContentType("application/json; charset=utf-8");
                response.getWriter().println(failMsg2);
                //response.sendRedirect(adminLoginPage);
                return false;
            }
            System.out.println("拦截器拦截的管理员：" + user + "  " + user.getUsername());
        }
        //只拦截/api/adminX/开头的接口
        if (uri.startsWith("/api/adminX/")) {
            AdminVO user = (AdminVO) httpSession.getAttribute("admin");
            if (user == null) {
                response.setContentType("application/json; charset=utf-8");
                response.getWriter().println(failMsg2);
                return false;
            }
            if (user.getRole() < 1) {
                response.setContentType("application/json; charset=utf-8");
                response.getWriter().println(failMsg3);
                return false;
            }
            System.out.println("拦截器拦截的超级管理员：" + user + "  " + user.getUsername());
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler,
                                Exception ex) throws Exception {

    }
}
